package org.viva.cms.sitemap;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import org.apache.velocity.VelocityContext;
import org.viva.api.VM;
import org.viva.service.sys.WebsiteCache;

public class BaiduSm {

//    final static String url = "http://data.zz.baidu.com/urls?site=www.dowdybear.com&token=X4tm1T1D2MiVCRHQ";// 网站的服务器连接

    /**
     * @param args
     */
//    public static void main(String[] args) {
//        String[] param = { "http://www.dowdybear.com/" };// 需要推送的网址
//        String json = Post(url, param);// 执行推送方法
//        System.out.println("结果是" + json); // 打印推送结果
//    }

    public static void art(String id) {
        try {
            String[] param = { WebsiteCache.get("sys.host") + "/a/" + id };
            String json = Post(WebsiteCache.get("baidu.zz.url"), param);// 执行推送方法
        } catch (Exception e) {
        }
    }

    public static void cg(String id) {
        try {
            String[] param = { WebsiteCache.get("sys.host") + "/g/" + id };
            String json = Post(WebsiteCache.get("baidu.zz.url"), param);// 执行推送方法
            System.out.println("结果是" + json); // 打印推送结果
        } catch (Exception e) {
        }
    }

    /**
     * 百度链接实时推送
     * 
     * @param PostUrl
     * @param Parameters
     * @return
     */
    public static String Post(String PostUrl, String[] Parameters) {
        if (null == PostUrl || null == Parameters || Parameters.length == 0) {
            return null;
        }
        String result = "";
        PrintWriter out = null;
        BufferedReader in = null;
        try {
            // 建立URL之间的连接
            URLConnection conn = new URL(PostUrl).openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("Host", "data.zz.baidu.com");
            conn.setRequestProperty("User-Agent", "curl/7.12.1");
            conn.setRequestProperty("Content-Length", "83");
            conn.setRequestProperty("Content-Type", "text/plain");

            // 发送POST请求必须设置如下两行
            conn.setDoInput(true);
            conn.setDoOutput(true);

            // 获取conn对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            // 发送请求参数
            String param = "";
            for (String s : Parameters) {
                param += s + "\n";
            }
            out.print(param.trim());
            // 进行输出流的缓冲
            out.flush();
            // 通过BufferedReader输入流来读取Url的响应
            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }

        } catch (Exception e) {
            System.out.println("发送post请求出现异常！" + e);
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }

            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }

    public synchronized static void createSitemapXml(List<SMURL> list, String path) throws IOException {
        VelocityContext context = new VelocityContext();
        context.put("list", list);
        String str = VM.vm("/vm/sitemap.xml", context);
        System.out.println(str);
        FileWriter fileWriter = new FileWriter(new File(path));
        try {
            fileWriter.write(str);
            fileWriter.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            fileWriter.close();
        }
    }

    public static class SMURL {
        private String loc;
        private String lastmod;
        private String changefreq;
        private String priority;

        public String getLoc() {
            return loc;
        }

        public void setLoc(String loc) {
            this.loc = loc;
        }

        public String getLastmod() {
            return lastmod;
        }

        public void setLastmod(String lastmod) {
            this.lastmod = lastmod;
        }

        public String getChangefreq() {
            return changefreq;
        }

        public void setChangefreq(String changefreq) {
            this.changefreq = changefreq;
        }

        public String getPriority() {
            return priority;
        }

        public void setPriority(String priority) {
            this.priority = priority;
        }

    }
}
